<template>
  <div>
    <div class="mb-3 text-right">
      <el-button
        type="primary"
        icon="el-icon-circle-plus-outline"
        @click.prevent="add"
      >
        添加
      </el-button>
    </div>
    <div class="mb-3">
      <el-table
        :data="tableData"
        height="calc(100vh - 14rem)"
        border
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="role" label="角色" width="180">
        </el-table-column>
        <el-table-column prop="desc" label="描述">
          <template #default="scope">
            <div class="truncate">{{ scope.row.desc }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作" width="120">
          <template #default="scope">
            <el-button type="text" @click.prevent="() => edit(scope.row)">
              编辑
            </el-button>
            <el-button type="text" @click.prevent="() => del(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      background
      @current-change="handleCurrentChange"
      :current-page="state.current_page"
      layout="total, prev, pager, next, jumper"
      :page-size="state.page_size"
      :total="state.total"
    >
    </el-pagination>
    <el-dialog
      :title="state.title"
      v-model="state.dialogFormVisible"
      width="700px"
    >
      <el-form :model="state.form" :rules="rules" ref="ruleForm">
        <el-form-item label="角色：" label-width="100px" prop="role">
          <el-input v-model="state.form.role" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述：" label-width="100px" prop="desc">
          <el-input v-model="state.form.desc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限：" label-width="100px">
          <el-tree
            class="overflow-auto h-96"
            :data="menus"
            show-checkbox
            node-key="id"
            :default-expand-all="true"
            :default-checked-keys="[11, 12, 13, 14]"
          ></el-tree>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="setVisible">取 消</el-button>
          <el-button type="primary" @click="submitForm"> 确 定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import { vocationalWorkRouter } from "@/router/router";
import arrayToTree from "array-to-tree";
import { moreDimensionalToOneDimensionalArray } from "@/utils";
import { ElMessageBox, ElMessage } from "element-plus";
const ruleForm = ref(null);
const rules = {
  role: [
    { required: true, message: "请输入角色名称", trigger: "blur" },
    { max: 20, message: "长度限制为20字符", trigger: "blur" },
  ],
  desc: [{ max: 40, message: "长度限制为40字符", trigger: "blur" }],
};

const menus = arrayToTree(
  moreDimensionalToOneDimensionalArray(vocationalWorkRouter[0].children).map(
    (item) => {
      item.label = item.meta.title;
      return item;
    }
  ),
  {
    parentProperty: "pid",
    customID: "id",
  }
);

const state = reactive({
  title: "添加角色",
  dialogFormVisible: false,
  form: {},

  total: 0,
  page_size: 20,
  current_page: 1,
});

const tableData = [];
for (let i = 1; i <= 20; i++) {
  tableData.push({
    id: i,
    role: "运营",
    desc: "APP运营相关工作",
  });
}

state.total = tableData.length;

function setVisible() {
  state.dialogFormVisible = !state.dialogFormVisible;
}

function add() {
  state.title = "添加角色";
  setVisible();
}

function submitForm() {
  ruleForm.value.validate((valid) => {
    if (valid) {
      console.log(state.form);
      alert("submit!");
    } else {
      console.log("error submit!!");
      return false;
    }
  });
}

function edit(row) {
  state.title = "编辑角色";
  setVisible();
}

function del(row) {
  ElMessageBox.confirm("确定删除该角色？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      ElMessage.success({
        message: "删除成功!",
      });
    })
    .catch(() => {
      ElMessage.info({
        message: "已取消删除",
      });
    });
}
function handleDel() {}
function handleCurrentChange(current_page) {
  state.current_page = current_page;
}
</script>
